<!-- 我的渠道 - 仅服务商可见 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键词" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="渠道名称/联系人/电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合作模式">
        <el-select v-model="queryParams.cooperateType" placeholder="请选择合作模式" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.cooperate_type"
            :key="Number(dict.value)"
            :label="dict.label"
            :value="Number(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" ref="table">
      <el-table-column label="渠道名称" align="center" prop="channelName" />
      <el-table-column label="渠道联系人" align="center" prop="contacts" />
      <el-table-column label="联系人电话" align="center" prop="phone" />
      <el-table-column label="合作模式" align="center" prop="cooperateType">
        <!-- 合作模式: 0无 1统一定价返佣 2采购价结算 -->
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.cooperate_type" :value="String(row.cooperateType)" />
        </template>
      </el-table-column>
      <el-table-column label="推广佣金比例" align="center" prop="commissionRatio">
        <template slot-scope="{row}">
          <div>{{ row.commissionRatio }}%</div>
        </template>
      </el-table-column>
      <el-table-column label="控价状态" align="center" prop="priceControl">
        <template slot-scope="{row}">
          <div>
            <dict-tag :options="dict.type.price_control" :value="String(row.priceControl)" />
            <span v-if="row.cooperateType === 2 && row.priceControl === 1">(加成{{row.priceControlLower}}%~{{row.priceControlUpper}}%)</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="合作时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOpen(scope.row)"
            >合作设置</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >解除合作</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditSet ref="EditSet" @refresh="getList" />
  </div>
</template>

<script>
import { getChannelList, deleteApplyCooperate } from '@/api/facilitator'
import EditSet from './editSet'

export default {
  name: "MyChannel",
  components: {
    EditSet
  },
  dicts: ['cooperate_type', 'price_control'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: "",
        cooperateType: undefined
      },
      // 总条数
      total: 0,
      dataList: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getChannelList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(row) {
      this.$refs.EditSet.openModal(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认要解绑(${row.channelName})？解绑后，渠道开发的机构将无法再使用您作为服务商。`)
        .then(function () {
          return deleteApplyCooperate({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("成功解绑");
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped></style>
